//
//  WBBaseController.swift
//  WB
//
//  Created by macssss on 16/6/29.
//  Copyright © 2016年 macssss. All rights reserved.
//

import UIKit

class WBBaseController: UIViewController {

    //创建TabelView
    var tabelView: UITableView?
    //下拉刷新
//    var refurbishControl: UIRefreshControl?
    var refreshControl: ZSRefreshControl?

    
    //区分上 下刷新数据
    var isPullup = false
    
    
    //访客视图信息
    var vistorsInfoDict:[String : String]?
    
    //自定义NavgationBar
    lazy var navigationBar  = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    //自定义标题
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        
        //        loadData()
        WBNetworkManager.shared.userLogon ? loadData() : ()
        

        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: NSNotification.Name(rawValue: WBUserLoginSuccessedNotification), object: nil)

    }
    
    
    deinit {
        
        //注销通知
        NotificationCenter.default.removeObserver(self)
    }
    
    
    
    
    override var title: String? {
        
        didSet {
            
            navItem.title = title
        }
        
    }
    
    //MARK: 如果子类不不实现方法 默认关闭刷新控件
    func loadData() {
        
        refreshControl?.endRefreshing()
    }

}


//MARK: - 访客视图登录按钮监听方法
extension WBBaseController {
    
    
    @objc private func loginSuccess(n: Notification) {
        
        
        view = nil
        
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        
        NotificationCenter.default.removeObserver(self)
        
    }

    
    @objc func longin() {
        
        print("登录")

        NotificationCenter.default.post(name: Notification.Name(rawValue: WBUserShouldLoginNotification), object: nil)

    }
    
    @objc func register() {
        
        print("注册")
    }
    
}

//MARK: -  TableView setupUI 方法————
extension WBBaseController {
    
   private func setupUI ()  {
    
//        view.backgroundColor = UIColor.white()
    
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavigationBar()
   
        WBNetworkManager.shared.userLogon ? setupTabelView() : setupVisitorsView()
    }
  
    
    //MARK: 设置表格视图
     func setupTabelView() {
        
        tabelView = UITableView(frame: view.bounds, style: .plain)
        
        //把tabelview 添加到自定义的Bar 下面
        view.insertSubview(tabelView!, belowSubview: navigationBar)
        
        //设置数据源
        tabelView?.dataSource = self
        tabelView?.delegate = self
        
        //把表格插入到导航条下面
        tabelView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height,
                                               left: 0,
                                               bottom: tabBarController?.tabBar.bounds.height ?? 49,
                                               right: 0)
        
        //修改指示器的缩进
        tabelView?.scrollIndicatorInsets = tabelView!.contentInset
        
        refreshControl = ZSRefreshControl()
        
        tabelView?.addSubview(refreshControl!)
        
        //添加监听方法
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        
        
    }
    
   //MARK: 设置访客视图
    private func setupVisitorsView() {
        
        let visitorsView = WBVisitorsView(frame: view.bounds)
    
            
        view.insertSubview(visitorsView, belowSubview: navigationBar)
    
        visitorsView.visitionInfo = vistorsInfoDict
        
        //MARK: - 设置访客视图中的按同样监听 tabar方法
        visitorsView.loginButton.addTarget(self, action: #selector(longin), for: .touchUpInside)
        visitorsView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        //监听方法
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(register))
        
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(longin))
    }
    
    
    //MARK: 设置标题字体
    private func setupNavigationBar() {
           
        view.addSubview(navigationBar)
        
        navigationBar.items = [navItem]
        
        navigationBar.tintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        //修改标题字体颜色
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray()]

        navigationBar.tintColor = UIColor.orange()
        
    }
}

//MARK: - 遵守协议方法实现数据源方法————

extension WBBaseController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 10
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
            
            return
        }
        
        //获取到行数
        let count = tableView.numberOfRows(inSection: section)
        
        //判断是不是最后一行 同时没有下拉刷新就上拉刷新
        if row == ( count - 1 ) && !isPullup {
            
          print("上拉刷新")
            
            isPullup = true
            
            loadData()
        }
        
        
        
    }

}
















